import React,{useState} from 'react';
import { Button,   Input, message } from 'antd';
import axios from 'axios';
import { useEffect } from 'react';
import baseaxiso  from '../../../src/axios/config'
import {redirect, useNavigate}from 'react-router-dom'
import './login.css'





function Index(){
    const navigate =useNavigate()
const [name,setname]=useState('')
const [password,setpd]=useState('')
const  [a1,seta1]=useState("")
const setusername=(e:any)=>{
    setname(e.target.value)
    console.log(e.target.value)
}
const setpassword =(e:any)=>{
setpd(e.target.value)
console.log(e.target.value)
}
const login = ()=>{

  baseaxiso.post("/login",{
      "username":name,
       "password":password
    }) . then(res=>{
    
          
         const data = res.data.id
           const c = "登录成功"
           const c1 = "用户名不对"
           const c2 = "密码不对"
 
      



    switch(data){

       case c:
 
       return  message.info("登录成功") ,  
       localStorage.setItem("token",res.data.token),
       navigate('/home');
      
       // eslint-disable-next-line no-unreachable
       break;
  
        case  c1 :
            return   message.info("用户名不对"), 
            redirect('/'),
            setTimeout(()=>{
                 // eslint-disable-next-line no-restricted-globals
                location.reload() ;
            },2000)
           
           
           
            // eslint-disable-next-line no-unreachable
            break;
                 
        case  c2:
            return  message.info("密码不对"),
        
        
            redirect('/'),
                // eslint-disable-next-line no-restricted-globals
                setTimeout(()=>{
                   
                    // eslint-disable-next-line no-restricted-globals
                    location.reload() ;
                },2000)
           
           
            break
    }

        }

       
    )
       
    }
             
             
    
   
useEffect(()=>{
    const token =    localStorage.getItem("token")
    if(token){
     navigate("/home")
    }  else{
     navigate("/")
    }
})
 

    return(
        <>
        <div  >
     <h1  >用户登录</h1>
 用户名:<Input  value={name}onChange={setusername}  style={{width:280}} /><br></br>
密码： <Input  value={password}onChange={setpassword}  style={{width:280}} /><br></br> 
 <Button type="primary" onClick={login}>登录</Button>
 </div>

</>
    )
}
export default Index